﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AjaxPro;

using DQS.EL;
using DQS.BLL;

public partial class Support_UserManage : DQS_Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Support_UserManage), this);
        if (!IsPostBack)
        {
            Data_Init();
        }
    }

    private void Data_Init()
    {
        Dept_No_DataBind();
        SetPager();
    }

    private void Dept_No_DataBind()
    {
        DEPT_NO_A.DataSource = DQ_OrganizationManager.SelectOrgaByLevel(4);
        DEPT_NO_A.DataTextField = "Orga_Name";
        DEPT_NO_A.DataValueField = "Orga_ID";
        DEPT_NO_A.DataBind();
    }

    protected void UserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Charge")
        {
            int result = DQ_USERSManager.ModifyStatus(Decimal.Parse(e.CommandArgument.ToString()));
            if (result == -1)
            {
                JQueryAlert("唯一已启用管理员禁止禁用！");
            }
            SetPager();
        }
        else if (e.CommandName == "Reset")
        {
            DQ_USERS user = new DQ_USERS();
            user.USER_ID = Decimal.Parse(e.CommandArgument.ToString());
            user.USER_PASS = Tool.EncryptPasswordByMD5(DefaultPass);
            DQ_USERSManager.ChangePassword(user);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success"
                , "$(function() {alert('密码重置成功。');})", true);
        }
    }
    protected void QueryUser_Click(object sender, EventArgs e)
    {
        SetPager();
    }
    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        DQ_USERS user = new DQ_USERS();
        SetUser(user);
        int effRow = DQ_USERSManager.AddDQ_USERS(user);
        if (effRow == 0)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "fail"
                , "$(function() {$('#AddUserDiv').dialog('open');alert('登录账号已存在!');})", true);
        }
        else
        {
            String loginName = user.USER_LOGIN;
            ResetAddForm();
            SetPager();         
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success"
                , "$(function() {alert('用户[" + loginName +"]已新增成功。');})", true);
        }        
    }

    private void SetUser(DQ_USERS user)
    {
        user.REAL_NAME = Real_Name_A.Text.Trim();
        int orga_id;
        Int32.TryParse(hidOrga.Value,out orga_id);
        orga_id = orga_id > 0 ? orga_id : Int32.Parse(DEPT_NO_A.SelectedValue);
        DQ_Organization orga = DQ_OrganizationManager.GetNameByOrgaID(orga_id);
        user.F_Orga_ID = orga;
        user.USER_PASS = Tool.EncryptPasswordByMD5(DefaultPass);
        user.USER_STATUS = "Y";
        /* -- modify the rule of user login 2012-04-06 by Aaron
         * user.USER_LOGIN = orga.Orga_Alias + "_" + Tool.GetChineseSpell(user.REAL_NAME);
         * */
        user.USER_LOGIN = User_Login.Text.Trim();
        user.IS_ADMIN = IS_ADMIN_A.Checked ? "Y" : "N";
    }

    protected void UserPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    private void ResetAddForm()
    {
        Real_Name_A.Text = "";
        User_Login.Text = "";
        DEPT_NO_A.SelectedIndex = 0;
        IS_ADMIN_A.Checked = false;
        Orga_Son.Items.Clear();
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        IList<DQ_USERS> list = DQ_USERSManager.SelectUsers(User_Login_I.Text.Trim().ToUpper());
        UserPager.RecordCount = list.Count;
        pds.DataSource = list;
        pds.AllowPaging = true;
        pds.PageSize = UserPager.PageSize;
        pds.CurrentPageIndex = UserPager.CurrentPageIndex - 1;        
        UserList.DataSource = pds;
        UserList.DataBind();
    }

    [AjaxMethod]
    public IList<DQ_Organization> GetSonAreaByParent(int argPid)
    {
        IList<DQ_Organization> orgas = DQ_OrganizationManager.GetSunByParent(argPid);
        if (orgas.Count > 0)
        {
            DQ_Organization neworga = new DQ_Organization();
            neworga.Orga_ID = 0;
            neworga.Orga_Name = "直属";
            orgas.Insert(0, neworga);
        }
        return orgas;
    }
}
